package com.itheima.health.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.itheima.health.common.Result;
import com.itheima.health.service.MemberService;
import com.itheima.health.utils.SendSms;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Random;

/**
 * @author cfr
 * @date 2022/1/3
 * @TIME:19:49
 */
@RestController
@RequestMapping("/validateCode")
public class ValidateCodeController {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private MemberService memberService;

    @PostMapping("/send4Login.do")
    public Result sendMsg1(String telephone){
        return sendMsg(telephone);
    }

    @PostMapping("/send4Order.do")
    public Result sendMsg2(String telephone){
        String attribute = (String) request.getSession().getAttribute(telephone);
        if(attribute==null){
            return new Result(false,"电话号码不存在");
        }
        return sendMsg(telephone);
    }


    private Result sendMsg(String telephone) {
        String code = "";
        for (int i = 0; i < 6; i++) {
            code += new Random().nextInt(9);
        }
        //SendSms.sendMsg(telephone,code );
        System.out.println(code);
        request.getSession().setAttribute(telephone, code);
        return new Result(true, "发送成功");
    }
}
